<template>
	<view>
		<view class="newsList">
			<view class="item" v-for="(item,index) in newsList" :key="index" @click="showDetail(item)">
				<view class="timeBlock">
					<view class="timeTag">
						{{item.time}}
					</view>
				</view>
				<view class="content">
					<view class="newsTitle">
						{{item.data.title}}
					</view>
					<view class="img">
						<van-image :src="item.data.img_src" width="650rpx" height="300rpx" fit="cover" radius="10rpx" />
					</view>
				</view>
			</view>
		</view>
		<van-notify id="van-notify" />
	</view>
</template>

<script>
	import Notify from '../../wxcomponents/vant/dist/notify/notify';
	export default {
		data() {
			return {
				page: 1,
				num: 5,
				newsList: [],
				firstTime: "",
				isComplete: false
			}
		},
		onLoad() {
			this.getFirstNews()
		},
		onReachBottom() {
			if (this.isComplete === false) {
				this.page++
				this.getOtherNews()
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getFirstNews()
		},
		methods: {
			showDetail(news) {
				uni.$on('sendNews', () => {
					uni.$emit('news', news)
				})
				uni.navigateTo({
					url: "../newsDetails/newsDetails"
				})
			},
			getOtherNews() {
				wx.showLoading({
					title: "加载中"
				})
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/news/getPageSchoolNews",
					data: {
						"page": self.page,
						"num": self.num,
						"time": self.firstTime
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.length === self.num) {
							let newList = self.newsList.concat(res.data)
							self.newsList = newList
						} else if (res.data.length > 0) {
							let newList = self.newsList.concat(res.data)
							self.newsList = newList
							self.isComplete = true
						} else {
							self.isComplete = true
						}
						wx.hideLoading();
					},
					fail: (err) => {
						wx.hideLoading();
						Notify({
							type: 'danger',
							message: '获取头条失败,请检查您的网络'
						});
					}
				})
			},
			getFirstNews() {
				let self = this
				wx.showLoading({
					title: "加载中"
				})
				uni.request({
					url: getApp().globalData.baseURL + "/news/getFirstPageSchoolNews",
					data: {
						"page": self.page,
						"num": self.num
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.list.length > 0) {
							self.firstTime = res.data.time
							self.newsList = res.data.list
						} else {
							Notify({
								type: 'danger',
								message: '获取头条失败,请检查您的网络'
							});
						}
						wx.hideLoading()
					},
					fail: (err) => {
						wx.hideLoading()
						Notify({
							type: 'danger',
							message: '获取头条失败,请检查您的网络'
						});
					}
				})
			}
		}
	}
</script>

<style scoped>
	.newsList .item {
		background-color: #F2F2F2;
		display: -webkit-box;
		display: flex;
		flex-direction: column;
		padding-bottom: 0;
	}

	.newsList .item .timeBlock {
		background-color: #F2F2F2;
		display: flex;
		height: 100rpx;
		align-items: center;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		justify-content: center;
	}

	.newsList .item .timeBlock .timeTag {
		color: #ffffff;
		background-color: #E6E6E6;
		border-radius: 20rpx;
		font-size: 25rpx;
		height: 40rpx;
		padding-top: 8rpx;
		padding-left: 25rpx;
		padding-right: 25rpx;
		justify-content: center;
		align-items: center;
	}

	.newsList .item .content {
		background-color: #ffffff;
		border-radius: 10rpx;
		display: -webkit-box;
		display: flex;
		flex-direction: column;
		margin-bottom: 0;
		margin-left: 25rpx;
		/* 	padding-left: 25rpx;
		padding-right: 25rpx; */
		padding: 25rpx;
		margin-right: 25rpx;
	}

	.newsList .item .content .newsTitle {
		font-size: 30rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
		line-height: 43rpx;
	}

	.newsList .item .content .img {
		margin-top: 10rpx;
	}
</style>
